package com.naiterui.ehp.bs.doctor.service.user.impl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.naiterui.ehp.bp.domain.Doctor;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.doctor.dao.IDoctorDao;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.fegin.EsignFeginClient;
import com.naiterui.ehp.bs.doctor.service.user.IDoctorAuthService;
import com.naiterui.ehp.bs.doctor.service.user.ILicenceService;
import com.naiterui.ehp.bs.doctor.vo.IdCardVO;
import com.naiterui.ehp.bs.doctor.vo.LicenseNewParam;
import com.naiterui.ehp.bs.doctor.vo.LicenseParam;


/**
 * 医生认证相关接口实现
 *
 * @author 王泽浩
 * @since 1.0.0
 */
@Service
public class AdDoctorAuthServiceImpl implements IDoctorAuthService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdDoctorAuthServiceImpl.class);
    public static final String SEAL_PATH_PRE_DR = ConfigUtil.getString("esign.seal.path") + "dr/";
    @Value("${file.base.prefix}")
    private String fileBasePath;

    @Autowired
    private IDoctorDao doctorDao;


    @Autowired
    private ILicenceService licenceService;

    @Autowired
    private EsignFeginClient esignFeginClient;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateAuth(Long doctorId, LicenseParam licenseParam)
        throws BusinessException {
        // 获取医生
        Doctor doctor = this.doctorDao.get(doctorId);

        // 医生不存在
        if (doctor == null) {
            throw new BusinessException(ExceptionCodes.USER_NOT_EXIST);
        }

        // 账户是否被禁用
        if (doctor.getAccountStatus() == 1) {
            throw new BusinessException(ExceptionCodes.USER_NOT_NOMAL);
        }

        Integer status = doctor.getStatus();
        if (Doctor.AUTH_STATUS_NOT_PERFECTION == status) {
            licenseParam.validate(true);
        }

        if (Doctor.AUTH_STATUS_AUDITING == status) {
            LOGGER.warn("医生处于审核中(或再次审核)，不能变更医生资料，医生ID:{}", doctorId);
            throw new BusinessException(ExceptionCodes.DOCTOR_AUTH_AUDIT);
        }
//        else if (Doctor.AUTH_STATUS_SUCCESS == status) {
//            LOGGER.warn("医生已经完成审核，医生ID id:{}", doctorId);
//            throw new BusinessException(ExceptionCodes.DOCTOR_AUTH_SUCCESS);
//        }

        // 上传图片
        this.licenceService.updateAuth(doctorId, licenseParam);
        // doctor.setStatus(Doctor.AUTH_STATUS_AUDITING);
        doctor.setSubmitAuditTime(new Date());
        doctor.setChangedAt(new Date());
        this.doctorDao.update(doctor);
    }

    @Override
    public void getRealnameCode(Long doctorId) throws BusinessException {
        Doctor doctor = this.getDoctor(doctorId);
        IdCardVO idCard = this.licenceService.getIdCardByDoctor(doctorId);
        if (idCard == null) {
            throw new BusinessException(ExceptionCodes.PRESCRIPTION_SIGN_DOCTOR_NOT_RECORD_ERROR);
        }
        this.esignFeginClient.getRealnameCode(doctor.getName(), doctor.getPhone(), idCard.getNumber());
    }

    @Override
    public void checkRealnameCode(Long doctorId, String code) throws BusinessException {
        Doctor doctor = this.getDoctor(doctorId);
        //实名验证，实名状态由签名服务维护
        this.esignFeginClient.checkRealnameByCode(doctor.getPhone(), code);
    }

    @Override
    public void updateAuthNew(Long doctorId, LicenseNewParam licenseNewParam) throws BusinessException {
        // 获取医生
        Doctor doctor = this.getDoctor(doctorId);

        Integer status = doctor.getStatus();

        if (Doctor.AUTH_STATUS_AUDITING == status) {
            LOGGER.warn("医生处于审核中(或再次审核)，不能变更医生资料，医生ID:{}", doctorId);
            throw new BusinessException(ExceptionCodes.DOCTOR_AUTH_AUDIT);
        }

        // 上传医生资质
        this.licenceService.updateAuthNew(doctorId, licenseNewParam);
        doctor.setSubmitAuditTime(new Date());
        doctor.setChangedAt(new Date());
        this.doctorDao.update(doctor);
    }

    private Doctor getDoctor(Long doctorId) throws BusinessException {
        Doctor doctor = this.doctorDao.get(doctorId);
        // 医生不存在
        if (doctor == null) {
            throw new BusinessException(ExceptionCodes.USER_NOT_EXIST);
        }

        // 账户是否被禁用
        if (doctor.getAccountStatus() == 1) {
            throw new BusinessException(ExceptionCodes.USER_NOT_NOMAL);
        }
        return doctor;
    }


}
